<!DOCTYPE html><html lang="zh-CN" data-theme="dark"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>Golang线性表-数组、切片 | J.のblog</title><meta name="keywords" content="云原生,golang"><meta name="author" content="J."><meta name="copyright" content="J."><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#0d0d0d"><meta name="description" content="线性表-数组、切片一、数据结构1、数值处理取整	&#x2F;&#x2F; 整除 	fmt.Println(3&#x2F;2, -1&#x2F;2, -3&#x2F;2) 	&#x2F;&#x2F; 向上取整 	fmt.Println(math.Ceil(2.01), math.Ceil(2.5), math.Ceil(2.9)) 	fmt.Println(math.Ceil(-2.01), math.Ceil(-2.5), math.Ceil(-2.9)) 	&#x2F;&#x2F;">
<meta property="og:type" content="article">
<meta property="og:title" content="Golang线性表-数组、切片">
<meta property="og:url" content="http://dycloudnavite.love/2023/02/12/Golang%E7%BA%BF%E6%80%A7%E8%A1%A8-%E6%95%B0%E7%BB%84%E3%80%81%E5%88%87%E7%89%87/index.html">
<meta property="og:site_name" content="J.のblog">
<meta property="og:description" content="线性表-数组、切片一、数据结构1、数值处理取整	&#x2F;&#x2F; 整除 	fmt.Println(3&#x2F;2, -1&#x2F;2, -3&#x2F;2) 	&#x2F;&#x2F; 向上取整 	fmt.Println(math.Ceil(2.01), math.Ceil(2.5), math.Ceil(2.9)) 	fmt.Println(math.Ceil(-2.01), math.Ceil(-2.5), math.Ceil(-2.9)) 	&#x2F;&#x2F;">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://dycloudnavite.love/img/golang.png">
<meta property="article:published_time" content="2023-02-12T06:32:00.000Z">
<meta property="article:modified_time" content="2023-08-30T02:50:41.248Z">
<meta property="article:author" content="J.">
<meta property="article:tag" content="云原生">
<meta property="article:tag" content="golang">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://dycloudnavite.love/img/golang.png"><link rel="shortcut icon" href="/dujie5416/img/touxiang.png"><link rel="canonical" href="http://dycloudnavite.love/2023/02/12/Golang%E7%BA%BF%E6%80%A7%E8%A1%A8-%E6%95%B0%E7%BB%84%E3%80%81%E5%88%87%E7%89%87/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/dujie5416/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/dujie5416/',
  algolia: {"appId":"9GWB2VV7GN","apiKey":"dac56ec5b0b1f45281251ee59db4d637","indexName":"blog","hits":{"per_page":6},"languages":{"input_placeholder":"搜索文章","hits_empty":"找不到您查询的内容：${query}","hits_stats":"找到 ${hits} 条结果，用时 ${time} 毫秒"}},
  localSearch: undefined,
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Golang线性表-数组、切片',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-08-30 10:50:41'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/xxx.css"><link rel="stylesheet" href="/css/universe.css"><link rel="stylesheet" href="/css/custom.css"><style type="text/css">#toggle-sidebar {bottom: 80px}</style><link rel="stylesheet" href="https://fastly.jsdelivr.net/gh/tzy13755126023/BLOG_SOURCE/css/function.min.css"><link rel="stylesheet" href="/css/rightMenu.css"><style type="text/css">#toggle-sidebar {left:100px}</style><!-- hexo injector head_end start --><link rel="stylesheet" href="https://cdn.cbd.int/hexo-butterfly-clock-anzhiyu/lib/clock.min.css" /><!-- hexo injector head_end end --><meta name="generator" content="Hexo 6.2.0"><link rel="alternate" href="/dujie5416/atom.xml" title="J.のblog" type="application/atom+xml">
</head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/dujie5416/img/touxiang.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/dujie5416/archives/"><div class="headline">文章</div><div class="length-num">73</div></a><a href="/dujie5416/tags/"><div class="headline">标签</div><div class="length-num">17</div></a><a href="/dujie5416/categories/"><div class="headline">分类</div><div class="length-num">11</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/dujie5416/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/dujie5416/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/dujie5416/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/dujie5416/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/dujie5416/img/longzhu.png')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/dujie5416/">J.のblog</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/dujie5416/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/dujie5416/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/dujie5416/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/dujie5416/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Golang线性表-数组、切片</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-02-12T06:32:00.000Z" title="发表于 2023-02-12 14:32:00">2023-02-12</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-08-30T02:50:41.248Z" title="更新于 2023-08-30 10:50:41">2023-08-30</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/dujie5416/categories/golang/">golang</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Golang线性表-数组、切片"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="线性表-数组、切片"><a href="#线性表-数组、切片" class="headerlink" title="线性表-数组、切片"></a>线性表-数组、切片</h1><h3 id="一、数据结构"><a href="#一、数据结构" class="headerlink" title="一、数据结构"></a>一、数据结构</h3><h5 id="1、数值处理"><a href="#1、数值处理" class="headerlink" title="1、数值处理"></a>1、数值处理</h5><h6 id="取整"><a href="#取整" class="headerlink" title="取整"></a>取整</h6><pre class="line-numbers language-go" data-language="go"><code class="language-go">	<span class="token comment">// 整除</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token operator">/</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token operator">/</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">3</span><span class="token operator">/</span><span class="token number">2</span><span class="token punctuation">)</span>
	<span class="token comment">// 向上取整</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>math<span class="token punctuation">.</span><span class="token function">Ceil</span><span class="token punctuation">(</span><span class="token number">2.01</span><span class="token punctuation">)</span><span class="token punctuation">,</span> math<span class="token punctuation">.</span><span class="token function">Ceil</span><span class="token punctuation">(</span><span class="token number">2.5</span><span class="token punctuation">)</span><span class="token punctuation">,</span> math<span class="token punctuation">.</span><span class="token function">Ceil</span><span class="token punctuation">(</span><span class="token number">2.9</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>math<span class="token punctuation">.</span><span class="token function">Ceil</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">2.01</span><span class="token punctuation">)</span><span class="token punctuation">,</span> math<span class="token punctuation">.</span><span class="token function">Ceil</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">2.5</span><span class="token punctuation">)</span><span class="token punctuation">,</span> math<span class="token punctuation">.</span><span class="token function">Ceil</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">2.9</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
	<span class="token comment">// 向下取整</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>math<span class="token punctuation">.</span><span class="token function">Floor</span><span class="token punctuation">(</span><span class="token number">2.31</span><span class="token punctuation">)</span><span class="token punctuation">,</span> math<span class="token punctuation">.</span><span class="token function">Floor</span><span class="token punctuation">(</span><span class="token number">2.54</span><span class="token punctuation">)</span><span class="token punctuation">,</span> math<span class="token punctuation">.</span><span class="token function">Floor</span><span class="token punctuation">(</span><span class="token number">2.99</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>math<span class="token punctuation">.</span><span class="token function">Floor</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">2.31</span><span class="token punctuation">)</span><span class="token punctuation">,</span> math<span class="token punctuation">.</span><span class="token function">Floor</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">2.54</span><span class="token punctuation">)</span><span class="token punctuation">,</span> math<span class="token punctuation">.</span><span class="token function">Floor</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">2.99</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
	<span class="token comment">// 四舍五入</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>math<span class="token punctuation">.</span><span class="token function">Round</span><span class="token punctuation">(</span><span class="token number">3.44449</span><span class="token punctuation">)</span><span class="token punctuation">,</span> math<span class="token punctuation">.</span><span class="token function">Round</span><span class="token punctuation">(</span><span class="token number">3.9</span><span class="token punctuation">)</span><span class="token punctuation">,</span> math<span class="token punctuation">.</span><span class="token function">Round</span><span class="token punctuation">(</span><span class="token number">3.555</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>math<span class="token punctuation">.</span><span class="token function">Round</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">3.44449</span><span class="token punctuation">)</span><span class="token punctuation">,</span> math<span class="token punctuation">.</span><span class="token function">Round</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">3.9</span><span class="token punctuation">)</span><span class="token punctuation">,</span> math<span class="token punctuation">.</span><span class="token function">Round</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">3.555</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token comment">// 输出结果为：</span>
<span class="token number">1</span> <span class="token number">0</span> <span class="token operator">-</span><span class="token number">1</span>
<span class="token number">3</span> <span class="token number">3</span> <span class="token number">3</span>
<span class="token operator">-</span><span class="token number">2</span> <span class="token operator">-</span><span class="token number">2</span> <span class="token operator">-</span><span class="token number">2</span>
~~~~~~~~~~~~~~~~
<span class="token number">2</span> <span class="token number">2</span> <span class="token number">2</span>
<span class="token operator">-</span><span class="token number">3</span> <span class="token operator">-</span><span class="token number">3</span> <span class="token operator">-</span><span class="token number">3</span>
~~~~~~~~~~~~~~~~
<span class="token number">3</span> <span class="token number">4</span> <span class="token number">4</span>
<span class="token operator">-</span><span class="token number">3</span> <span class="token operator">-</span><span class="token number">4</span> <span class="token operator">-</span><span class="token number">4</span>
~~~~~~~~~~~~~~~~<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<ul>
<li>&#x2F;整除除法，截取整数部分</li>
<li>math.Ceil向上取整</li>
<li>math.Floor向下取整</li>
<li>math.Round 四舍五入</li>
</ul>
<h5 id="其他数值处理"><a href="#其他数值处理" class="headerlink" title="其他数值处理"></a>其他数值处理</h5><pre class="line-numbers language-go" data-language="go"><code class="language-go">fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>math<span class="token punctuation">.</span><span class="token function">Abs</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">2.7</span><span class="token punctuation">)</span><span class="token punctuation">)</span>                               <span class="token comment">// 绝对值</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>math<span class="token punctuation">.</span>E<span class="token punctuation">,</span> math<span class="token punctuation">.</span>Pi<span class="token punctuation">)</span>                              <span class="token comment">// 常数</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>math<span class="token punctuation">.</span>MaxInt16<span class="token punctuation">,</span> math<span class="token punctuation">.</span>MinInt16<span class="token punctuation">)</span>                 <span class="token comment">// 常量，极值</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>math<span class="token punctuation">.</span><span class="token function">Log10</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">,</span> math<span class="token punctuation">.</span><span class="token function">Log2</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">)</span>                <span class="token comment">// 对数</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>math<span class="token punctuation">.</span><span class="token function">Max</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span> math<span class="token punctuation">.</span><span class="token function">Min</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">)</span>              <span class="token comment">// 最大值、最小值</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>math<span class="token punctuation">.</span><span class="token function">Pow</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">,</span> math<span class="token punctuation">.</span><span class="token function">Pow10</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">)</span>                <span class="token comment">// 幂</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>math<span class="token punctuation">.</span><span class="token function">Mod</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token operator">%</span><span class="token number">2</span><span class="token punctuation">)</span>                          <span class="token comment">// 取模</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>math<span class="token punctuation">.</span><span class="token function">Sqrt</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span> math<span class="token punctuation">.</span><span class="token function">Sqrt</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">,</span> math<span class="token punctuation">.</span><span class="token function">Pow</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">0.5</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">// 开方</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>


<h5 id="标准输入"><a href="#标准输入" class="headerlink" title="标准输入"></a>标准输入</h5><p>Scan：空表字符分割，回车提交。换行当做空白字符</p>
<pre class="line-numbers language-go" data-language="go"><code class="language-go"><span class="token keyword">package</span> main
<span class="token keyword">import</span> <span class="token punctuation">(</span>
 <span class="token string">"fmt"</span>
<span class="token punctuation">)</span>
<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
 <span class="token keyword">var</span> n <span class="token builtin">int</span>
 <span class="token keyword">var</span> err <span class="token builtin">error</span>
 <span class="token keyword">var</span> word1<span class="token punctuation">,</span> word2 <span class="token builtin">string</span>
 fmt<span class="token punctuation">.</span><span class="token function">Print</span><span class="token punctuation">(</span><span class="token string">"Plz input two words: "</span><span class="token punctuation">)</span>
 n<span class="token punctuation">,</span> err <span class="token operator">=</span> fmt<span class="token punctuation">.</span><span class="token function">Scan</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>word1<span class="token punctuation">,</span> <span class="token operator">&amp;</span>word2<span class="token punctuation">)</span> <span class="token comment">// 控制台输入时，单词之间空白字符分割</span>
 <span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">&#123;</span>
 <span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
 <span class="token punctuation">&#125;</span>
 fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span>
 fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"%T %s, %T %s\n"</span><span class="token punctuation">,</span> word1<span class="token punctuation">,</span> word1<span class="token punctuation">,</span> word2<span class="token punctuation">,</span> word2<span class="token punctuation">)</span>
 fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">"~~~~~~~~~~~~~~~~~~~~~~~~~~~"</span><span class="token punctuation">)</span>
 <span class="token keyword">var</span> i1<span class="token punctuation">,</span> i2 <span class="token builtin">int</span>
 fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">"Plz input two ints: "</span><span class="token punctuation">)</span>
 n<span class="token punctuation">,</span> err <span class="token operator">=</span> fmt<span class="token punctuation">.</span><span class="token function">Scan</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>i1<span class="token punctuation">,</span> <span class="token operator">&amp;</span>i2<span class="token punctuation">)</span>
 <span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">&#123;</span>
 <span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
 <span class="token punctuation">&#125;</span>
 fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span>
 fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"%T %[1]d, %T %[2]d"</span><span class="token punctuation">,</span> i1<span class="token punctuation">,</span> i2<span class="token punctuation">)</span> <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>如果少一个数据，Scan就会阻塞；如果输入数据多了，等下回Scan读取。例如，一次性输入a b 1 2看看效果。</p>
<p>Scanf：读取输入，按照格式匹配解析。如果解析失败，立即报错，那么就会影响后面的Scanf。</p>
<pre class="line-numbers language-go" data-language="go"><code class="language-go"><span class="token keyword">package</span> main
<span class="token keyword">import</span> <span class="token punctuation">(</span>
 <span class="token string">"fmt"</span>
<span class="token punctuation">)</span>
<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
 <span class="token keyword">var</span> n <span class="token builtin">int</span>
 <span class="token keyword">var</span> err <span class="token builtin">error</span>
 <span class="token keyword">var</span> name <span class="token builtin">string</span>
 <span class="token keyword">var</span> age <span class="token builtin">int</span>
 fmt<span class="token punctuation">.</span><span class="token function">Print</span><span class="token punctuation">(</span><span class="token string">"Plz input your name and age: "</span><span class="token punctuation">)</span>
 n<span class="token punctuation">,</span> err <span class="token operator">=</span> fmt<span class="token punctuation">.</span><span class="token function">Scanf</span><span class="token punctuation">(</span><span class="token string">"%s %d\n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>name<span class="token punctuation">,</span> <span class="token operator">&amp;</span>age<span class="token punctuation">)</span> <span class="token comment">// 这里要有\n以匹配回车</span>
 <span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">&#123;</span>
 <span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
 <span class="token punctuation">&#125;</span>
 fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>n<span class="token punctuation">,</span> name<span class="token punctuation">,</span> age<span class="token punctuation">)</span>
 <span class="token keyword">var</span> weight<span class="token punctuation">,</span> height <span class="token builtin">int</span>
 fmt<span class="token punctuation">.</span><span class="token function">Print</span><span class="token punctuation">(</span><span class="token string">"weight and height: "</span><span class="token punctuation">)</span>
 <span class="token boolean">_</span><span class="token punctuation">,</span> err <span class="token operator">=</span> fmt<span class="token punctuation">.</span><span class="token function">Scanf</span><span class="token punctuation">(</span><span class="token string">"%d %d"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>weight<span class="token punctuation">,</span> <span class="token operator">&amp;</span>height<span class="token punctuation">)</span>
 <span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">&#123;</span>
 <span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
 <span class="token punctuation">&#125;</span>
 fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"%T %[1]d, %T %[2]d"</span><span class="token punctuation">,</span> weight<span class="token punctuation">,</span> height<span class="token punctuation">)</span> <span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>


<p>fmt.Scanf(“%s,%d”, &amp;name, &amp;age) 中%s会和后面的非空白字符分不清楚，用 abc,20 是匹配不上的，因为除空白字符外，都可以看做是字符串。所以，建议格式字符串中，一律使用空格等空白字符分割。</p>
<h3 id="二、线性数据结构"><a href="#二、线性数据结构" class="headerlink" title="二、线性数据结构"></a>二、线性数据结构</h3><p>线性表：</p>
<ul>
<li>线性表（简称表）：是一种抽象的概念，是一组元素的序列的抽象，它由又穷个元素组成（0个或任意个）</li>
</ul>
<p>物理实现有两种方式：</p>
<ul>
<li><strong>顺序表</strong>：使用一大块连续的内存顺序存储表中的元素，这样的表称为顺序表，或成为连续表（go语言中的数组）<ul>
<li>在顺序表中，元素的关系使用顺序表的存储顺序自然的表示</li>
<li>顺序表需要开辟连续的内存空间，变量指向顺序表的第一个元素地址</li>
<li>变量指向顺序表A&#x3D;[1,2,3,4,5] A这个标识符指向数组的首地址，每个元素占用空间一样大</li>
</ul>
</li>
</ul>
<p>从CRUD的方面来分析：</p>
<ul>
<li>C：create<ul>
<li>容器元素个数+1</li>
<li>append如同排队，在队尾增加</li>
<li>insert<ul>
<li>中间增加，把当前位置与其后所有元素向后移动</li>
<li>尾部增加，就相当于append</li>
<li>开头增加，所有元素向后移动</li>
<li>挪动数据是要消耗时间，挪动的元素越多（规模越大），代价也就越大</li>
</ul>
</li>
</ul>
</li>
<li>D：delete<ul>
<li>容器元素个数-1</li>
<li>队尾移除pop，影响最小</li>
<li>中间移除remove<ul>
<li>如果是队尾，就相当于pop</li>
<li>中间移除，后面的数据都向前移动</li>
<li>开头删除，所有元素向前移动</li>
</ul>
</li>
</ul>
</li>
<li>U：update<ul>
<li>元素个数len不变</li>
<li>首先定位需要修改的元素</li>
<li>更新内容</li>
</ul>
</li>
<li>R：read<ul>
<li>定位元素：首地址 + 该类型的字节数 * 偏移量</li>
<li>偏移，所以定位要用索引计算得到元素的内存地址，不用遍历，效率极高。如果使用内容定位，内容比较，就需要遍历的方式查找，效率低下</li>
<li>获取内容：使用索引定位直接该位置，拿走内容</li>
<li>遍历：容器的元素，不管有没有顺序，都需要不重复的将元素挨个摸一遍，首地址开始挨个偏移取内容</li>
</ul>
</li>
</ul>
<p>前提：要看数据规模，如果数据规模小，随意使用，顺序表适合在尾部增删，且有扩容问题。</p>
<ul>
<li><strong>链接表</strong>：每一个元素存储在内存中，但是元素并不是连续的的存储在内存中，散落在内存的不同位置，前一个元素指向下一个元素（链接），head首部元素，tail尾部元素<ul>
<li>链表是个容器，可以放元素，不需要实现开辟内存</li>
<li>单项链表：前一个元素指向下一个元素</li>
<li>双向链表：前一个元素指向下一个元素，下一个元素也指向上一个元素</li>
<li>列表（List）往往都是链表实现。python例外</li>
</ul>
</li>
</ul>
<p>从CRUD的方面来分析：</p>
<ul>
<li>C：create<ul>
<li>容器元素个数+1</li>
<li>尾部追加，原来的尾巴指向新尾巴，容器改tail指向，没有数据挪动，速度很快</li>
<li>中间插入，断开原来的链接，分别和新元素拉手，没有数据挪动，代价不大</li>
<li>首部插入，原来的头和新头互指，当前head的下一个元素，成为新head，删除旧head元素</li>
</ul>
</li>
<li>D：delete<ul>
<li>容器元素个数 -1</li>
<li>尾部删除：用tail定位尾巴，删除当前尾巴，当前尾巴的前一个称为新tail，删除旧tail元素</li>
<li>中间删除：遍历定位，当前元素删除，前驱、后继拉手即可</li>
<li>首部删除：用head定位首部，当前head的下一个元素，成为新head，删除旧head元素</li>
</ul>
</li>
<li>R：read<ul>
<li>定位元素：<ul>
<li>有索引，使用索引定位，由于元素散落在内存中，不能使用顺序表的公式来定位，找到开头，依次编索引，按照遍历元素的方式来找的，但是取的每一个节点中保存的下一个地址，使用该地址定位下一个元素，和顺序表比起来，略慢，但是由于都是使用地址，效率还不错。</li>
<li>使用内容定位，需要遍历查找，效率低下</li>
<li>获取内容：定位到了，取走内容即可</li>
</ul>
</li>
</ul>
</li>
<li>U：update<ul>
<li>元素个数len不变</li>
<li>定位问题</li>
<li>更新内容</li>
</ul>
</li>
</ul>
<p>链接表在规模大时，如果增删发生在中间或头部（可以包括尾部），链接表比较适合，因为他不需要扩容，找到一个内存中能放当前元素即可，然后修改指向（链接地址）</p>
<p>数组等类型，如同地铁站排好的队伍，有序，可以插队、离队，可以索引。</p>
<p>链表，如同操场上手拉手的小朋友，有序，但排列随意，或者可以想象成一串带线的珠子，随意盘方在桌上。也可以离队、插队，也可以索引。</p>
<h3 id="三、数组"><a href="#三、数组" class="headerlink" title="三、数组"></a>三、数组</h3><p>数组是由顺序表实现，容器不可变，可以索引，是值类型，容器的元素个数定义后就不能变了。数组必须给出长度，以后数组这个容器就不能增删元素，不能扩容了。</p>
<p>数组的内存结构：</p>
<p>数组的地址就是数组内第一个元素的内存地址</p>
<p>每个元素占用的空间看元素类型，int动态类型，看cpu架构，32位就是4字节，64位就是8字节。</p>
<p>一个元素占用几个字节和类型有关，第一个元素后面一定是第二个元素的存储单元，顺序表</p>
<p>字符串字面常来那个，一旦定义就不可改变，但是不同的字符串长度不一，数组采用string为元素，元素的存储空间间隔一样，都是16字节(字符串在go语言中也是个结构体，指向字符串的指针为byte类型也就是uint8，占用8字节，字符串长度为int，也占用8字节)，说明字符串更复杂。（<strong>16字节中其实是放的指向字符串的指针，而并不是字符串本身！</strong>）</p>
<pre class="line-numbers language-go" data-language="go"><code class="language-go"><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<h5 id="定义"><a href="#定义" class="headerlink" title="定义"></a>定义</h5><pre class="line-numbers language-go" data-language="go"><code class="language-go"><span class="token comment">// 注意下面2种区别</span>
<span class="token keyword">var</span> a0 <span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token builtin">int</span>                   <span class="token comment">// 零值初始化3个元素的数组</span>
<span class="token keyword">var</span> a1 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token builtin">int</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span>               <span class="token comment">// 零值初始化3个元素的数组</span>

<span class="token comment">// [3]int是类型，[3]int&#123;&#125; 是字面量值</span>
<span class="token keyword">var</span> a2 <span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token builtin">int</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token builtin">int</span><span class="token punctuation">&#123;</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">&#125;</span> <span class="token comment">// 声明且初始化，不推荐，啰嗦</span>
<span class="token keyword">var</span> a3 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token builtin">int</span><span class="token punctuation">&#123;</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">&#125;</span>        <span class="token comment">// 声明且初始化，推荐</span>
count <span class="token operator">:=</span> <span class="token number">3</span>
a4 <span class="token operator">:=</span> <span class="token punctuation">[</span>count<span class="token punctuation">]</span> <span class="token builtin">int</span><span class="token punctuation">&#123;</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">&#125;</span> <span class="token comment">// 错误的长度类型，必须是常量，换成const</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>a2<span class="token punctuation">,</span> a3<span class="token punctuation">)</span>
<span class="token keyword">const</span> count <span class="token operator">=</span> <span class="token number">3</span>
a4 <span class="token operator">:=</span> <span class="token punctuation">[</span>count<span class="token punctuation">]</span><span class="token builtin">int</span><span class="token punctuation">&#123;</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">&#125;</span> <span class="token comment">// 正确</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>a2<span class="token punctuation">,</span> a3<span class="token punctuation">,</span> a4<span class="token punctuation">)</span>
a5 <span class="token operator">:=</span> <span class="token punctuation">[</span><span class="token operator">...</span><span class="token punctuation">]</span><span class="token builtin">int</span> <span class="token punctuation">&#123;</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">30</span><span class="token punctuation">,</span> <span class="token number">50</span><span class="token punctuation">&#125;</span> <span class="token comment">// ...让编译器确定当前数组大小</span>
a6 <span class="token operator">:=</span> <span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token builtin">int</span><span class="token punctuation">&#123;</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">&#125;</span>       <span class="token comment">// 顺序初始化前面的，其余用零值填充</span>
a7 <span class="token operator">:=</span> <span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token builtin">int</span><span class="token punctuation">&#123;</span><span class="token number">1</span><span class="token punctuation">:</span> <span class="token number">300</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">:</span> <span class="token number">400</span><span class="token punctuation">&#125;</span> <span class="token comment">// 指定索引位置初始化，其余用零值填充</span>
<span class="token comment">// 二维数组</span>
a8 <span class="token operator">:=</span> <span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token builtin">int</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#123;</span><span class="token number">100</span><span class="token punctuation">&#125;</span><span class="token punctuation">&#125;</span> <span class="token comment">// 两行三列 [[100 0 0] [0 0 0]]</span>
<span class="token comment">// [[10 0 0] [11 12 0] [13 14 15] [16 0 0]]</span>
<span class="token comment">// 多维数组，只有第一维才能用...推测</span>
<span class="token comment">// 第一维有4个，第二维有3个。可以看做4行3列的表</span>
a9 <span class="token operator">:=</span> <span class="token punctuation">[</span><span class="token operator">...</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token builtin">int</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#123;</span><span class="token number">10</span><span class="token punctuation">&#125;</span><span class="token punctuation">,</span> <span class="token punctuation">&#123;</span><span class="token number">11</span><span class="token punctuation">,</span> <span class="token number">12</span><span class="token punctuation">&#125;</span><span class="token punctuation">,</span> <span class="token punctuation">&#123;</span><span class="token number">13</span><span class="token punctuation">,</span> <span class="token number">14</span><span class="token punctuation">,</span> <span class="token number">15</span><span class="token punctuation">&#125;</span><span class="token punctuation">,</span> <span class="token punctuation">&#123;</span><span class="token number">16</span><span class="token punctuation">&#125;</span><span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h5 id="长度和容量"><a href="#长度和容量" class="headerlink" title="长度和容量"></a>长度和容量</h5><ul>
<li>cap即capacity，容量，表示给数组分配的内存空间可以容纳多少个元素</li>
<li>len即length，长度，指的是容器中目前有几个元素</li>
</ul>
<p>由于数组创建时就必须确定元素的个数，且不能改变长度，所以不需要预留多余的内存空间，因此cap和len对数组来说相等。</p>
<h5 id="索引"><a href="#索引" class="headerlink" title="索引"></a>索引</h5><p>Go语言不支持负索引，通过[index]来获取该位置上的值。索引范围就是[0,长度-1]</p>
<h5 id="修改"><a href="#修改" class="headerlink" title="修改"></a>修改</h5><pre class="line-numbers language-go" data-language="go"><code class="language-go">a5 <span class="token operator">:=</span> <span class="token punctuation">[</span><span class="token operator">...</span><span class="token punctuation">]</span><span class="token builtin">int</span><span class="token punctuation">&#123;</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">30</span><span class="token punctuation">,</span> <span class="token number">50</span><span class="token punctuation">&#125;</span>
a5<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">+=</span> <span class="token number">100</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<h5 id="遍历"><a href="#遍历" class="headerlink" title="遍历"></a>遍历</h5><ul>
<li>索引遍历</li>
</ul>
<pre class="line-numbers language-go" data-language="go"><code class="language-go">a1 <span class="token operator">:=</span> <span class="token punctuation">[</span><span class="token operator">...</span><span class="token punctuation">]</span><span class="token builtin">int</span><span class="token punctuation">&#123;</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">30</span><span class="token punctuation">,</span> <span class="token number">50</span><span class="token punctuation">&#125;</span>
<span class="token keyword">for</span> i <span class="token operator">:=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token function">len</span><span class="token punctuation">(</span>a1<span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span> <span class="token punctuation">&#123;</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> a1<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>
<ul>
<li>for-range遍历</li>
</ul>
<pre class="line-numbers language-go" data-language="go"><code class="language-go"><span class="token keyword">for</span> i<span class="token punctuation">,</span> i2 <span class="token operator">:=</span> <span class="token keyword">range</span> a1 <span class="token punctuation">&#123;</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> i2<span class="token punctuation">)</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>
<h5 id="内存模型"><a href="#内存模型" class="headerlink" title="内存模型"></a>内存模型</h5><pre class="line-numbers language-go" data-language="go"><code class="language-go">	<span class="token keyword">var</span> a <span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token builtin">int</span> <span class="token comment">// 内存开辟空间存放长度为3的数组，零值填充</span>
	<span class="token keyword">for</span> i <span class="token operator">:=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token function">len</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span> <span class="token punctuation">&#123;</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> a<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>a<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>
	<span class="token punctuation">&#125;</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"%p %p %v \n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>a<span class="token punctuation">,</span> <span class="token operator">&amp;</span>a<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> a<span class="token punctuation">)</span>
	a<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1000</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"%p %p %v \n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>a<span class="token punctuation">,</span> <span class="token operator">&amp;</span>a<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> a<span class="token punctuation">)</span>
<span class="token comment">//输出结果</span>
<span class="token number">0</span> <span class="token number">0</span> <span class="token number">0x1400001c090</span>
<span class="token number">1</span> <span class="token number">0</span> <span class="token number">0x1400001c098</span>
<span class="token number">2</span> <span class="token number">0</span> <span class="token number">0x1400001c0a0</span>
<span class="token number">0x1400001c090</span> <span class="token number">0x1400001c090</span> <span class="token punctuation">[</span><span class="token number">0</span> <span class="token number">0</span> <span class="token number">0</span><span class="token punctuation">]</span> 
<span class="token number">0x1400001c090</span> <span class="token number">0x1400001c090</span> <span class="token punctuation">[</span><span class="token number">1000</span> <span class="token number">0</span> <span class="token number">0</span><span class="token punctuation">]</span> <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<ul>
<li>数组必须在编译时就确定大小，之后不能改变大小</li>
<li>数组第一个元素的地址就是数组地址</li>
<li>所有元素一个接一个顺序存储在内存中</li>
<li>元素的值可以以改变，但是元素地址不变</li>
</ul>
<p>上面每个元素间隔8个字节，正好64位，符合int类型定义</p>
<p>如果数据元素是字符串类型呢？</p>
<pre class="line-numbers language-go" data-language="go"><code class="language-go">	<span class="token keyword">var</span> a <span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token builtin">string</span>  <span class="token comment">// 内存开辟空间存放长度为3的数组</span>
	<span class="token keyword">for</span> i <span class="token operator">:=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token function">len</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span> <span class="token punctuation">&#123;</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> a<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>a<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>
	<span class="token punctuation">&#125;</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"%p %p %v \n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>a<span class="token punctuation">,</span> <span class="token operator">&amp;</span>a<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> a<span class="token punctuation">)</span>
	a<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">"oooooooooo"</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"%p %p %v \n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>a<span class="token punctuation">,</span> <span class="token operator">&amp;</span>a<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> a<span class="token punctuation">)</span>
<span class="token comment">// 运行结果</span>
<span class="token number">0</span>  <span class="token number">0x1400006c180</span>
<span class="token number">1</span>  <span class="token number">0x1400006c190</span>
<span class="token number">2</span>  <span class="token number">0x1400006c1a0</span>
<span class="token number">0x1400006c180</span> <span class="token number">0x1400006c180</span> <span class="token punctuation">[</span>  <span class="token punctuation">]</span> 
<span class="token number">0x1400006c180</span> <span class="token number">0x1400006c180</span> <span class="token punctuation">[</span>ss  <span class="token punctuation">]</span> <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<ul>
<li>数组首地址就是数组地址</li>
<li>所有元素顺序存储在内存中</li>
<li>元素的值可以改变，但是元素地址不变</li>
</ul>
<h5 id="值类型"><a href="#值类型" class="headerlink" title="值类型"></a><strong>值类型</strong></h5><pre class="line-numbers language-go" data-language="go"><code class="language-go"><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token string">"fmt"</span>

<span class="token keyword">func</span> <span class="token function">showAddr</span><span class="token punctuation">(</span>arr <span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token builtin">int</span> <span class="token punctuation">&#123;</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"arr = %v  %p\n"</span><span class="token punctuation">,</span> arr<span class="token punctuation">,</span> <span class="token operator">&amp;</span>arr<span class="token punctuation">)</span>
	<span class="token keyword">return</span> arr
<span class="token punctuation">&#125;</span>
<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
	a1 <span class="token operator">:=</span> <span class="token punctuation">[</span><span class="token operator">...</span><span class="token punctuation">]</span><span class="token builtin">int</span><span class="token punctuation">&#123;</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">30</span><span class="token punctuation">,</span> <span class="token number">50</span><span class="token punctuation">&#125;</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"a1 = %v ,%p\n"</span><span class="token punctuation">,</span> a1<span class="token punctuation">,</span> <span class="token operator">&amp;</span>a1<span class="token punctuation">)</span>
	a2 <span class="token operator">:=</span> a1
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"a2 = %v ,%p\n"</span><span class="token punctuation">,</span> a2<span class="token punctuation">,</span> <span class="token operator">&amp;</span>a2<span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"</span><span class="token punctuation">)</span>
	a3 <span class="token operator">:=</span> <span class="token function">showAddr</span><span class="token punctuation">(</span>a1<span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"a3 = %v ,%p\n"</span><span class="token punctuation">,</span> a3<span class="token punctuation">,</span> <span class="token operator">&amp;</span>a3<span class="token punctuation">)</span>
<span class="token punctuation">&#125;</span>
<span class="token comment">// 输出结果</span>
a1 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">10</span> <span class="token number">30</span> <span class="token number">50</span><span class="token punctuation">]</span> <span class="token punctuation">,</span><span class="token number">0x140000c0018</span>
a2 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">10</span> <span class="token number">30</span> <span class="token number">50</span><span class="token punctuation">]</span> <span class="token punctuation">,</span><span class="token number">0x140000c0048</span>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">10</span> <span class="token number">30</span> <span class="token number">50</span><span class="token punctuation">]</span>  <span class="token number">0x140000c0090</span>
a3 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">10</span> <span class="token number">30</span> <span class="token number">50</span><span class="token punctuation">]</span> <span class="token punctuation">,</span><span class="token number">0x140000c0078</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>可以看到a1、a2、a3、a4的地址都不一样，看最后a2 :&#x3D;a1之后两个变量地址也不一样。</p>
<p>这说明go语言在这些地方对数组进行了值拷贝，都生成了一份副本。</p>
<h3 id="四、切片"><a href="#四、切片" class="headerlink" title="四、切片"></a>四、切片</h3><ul>
<li>容量可变</li>
<li>内容可变</li>
<li>引用类型，和值类型有区别</li>
<li>底层基于数组，依赖于顺序表，表现得也像个可变容量和长度顺序表</li>
</ul>
<p>####### 内存模型</p>
<ul>
<li>组合结构<ul>
<li>1、底层数组，数组容量不可变，元素内容可变</li>
<li>2、slice head标头值或descriptor描述符</li>
</ul>
</li>
</ul>
<p>切片本质是对底层数组一个<strong>连续片段</strong>的引用。此片段可以是整个底层数组，也可以是由起始和终止索引标识的一些项的子集</p>
<pre class="line-numbers language-go" data-language="go"><code class="language-go"><span class="token keyword">type</span> slice <span class="token keyword">struct</span> <span class="token punctuation">&#123;</span>
	array unsafe<span class="token punctuation">.</span>Pointer  <span class="token comment">//指针，指向低层数组点的首地址</span>
	<span class="token builtin">len</span>   <span class="token builtin">int</span>    <span class="token comment">// 当前切片的长度</span>
	<span class="token builtin">cap</span>   <span class="token builtin">int</span>    <span class="token comment">//当前切片的容量</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>上面三个结构体的属性都是小写，所以包外不可见。len函数取的就是len属性，cap取得就是cap属性</p>
<p>指针可以通过取底层数组的第一个元素的地址，即切片第一个元素的地址</p>
<pre class="line-numbers language-go" data-language="go"><code class="language-go">	a <span class="token operator">:=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">int</span><span class="token punctuation">&#123;</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">&#125;</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"%v, %p  %p"</span><span class="token punctuation">,</span> a<span class="token punctuation">,</span> <span class="token operator">&amp;</span>a<span class="token punctuation">,</span> <span class="token operator">&amp;</span>a<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token comment">//输出</span>
<span class="token punctuation">[</span><span class="token number">1</span> <span class="token number">3</span> <span class="token number">5</span> <span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token number">0x1400000c030</span>  <span class="token number">0x1400001a020</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>
<p>&amp;a 是切片结构体的地址，&amp;a[0]是底层数组的地址。</p>
<p><img src="/images/28TXXYbu3gsTwGy68ynN8cZubdHgBOHGzM6_BtuHWn0.png" alt="image"></p>
<p>####### 切片定义</p>
<pre class="line-numbers language-go" data-language="go"><code class="language-go"><span class="token keyword">var</span> s1 <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">int</span>   <span class="token comment">// 长度、容量都为0的切片，零值</span>
<span class="token keyword">var</span> s2 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">int</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span>  <span class="token comment">// 长度、容量都为0的切片，字面量定义</span>
<span class="token keyword">var</span> s3 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">int</span><span class="token punctuation">&#123;</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">&#125;</span> <span class="token comment">//长度、容量都为3点的切片</span>
<span class="token keyword">var</span> s4 <span class="token operator">=</span> <span class="token function">make</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">int</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token comment">// 长度、容量都为0的切片，make([]T,length)</span>
<span class="token keyword">var</span> s5 <span class="token operator">=</span> <span class="token function">make</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">int</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">)</span> <span class="token comment">// 长度为1，容量为5，底层数组长度为5，元素长度为1，所以显示[0]</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h5 id="切片删除（伪删除）"><a href="#切片删除（伪删除）" class="headerlink" title="切片删除（伪删除）"></a>切片删除（伪删除）</h5><pre class="line-numbers language-go" data-language="go"><code class="language-go"><span class="token keyword">func</span> <span class="token function">Remove</span><span class="token punctuation">(</span>slice <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">int</span><span class="token punctuation">,</span> index <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">int</span> <span class="token punctuation">&#123;</span>
    <span class="token keyword">return</span> <span class="token function">append</span><span class="token punctuation">(</span>slice<span class="token punctuation">[</span><span class="token punctuation">:</span>index<span class="token punctuation">]</span><span class="token punctuation">,</span> slice<span class="token punctuation">[</span>index<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token punctuation">]</span><span class="token operator">...</span><span class="token punctuation">)</span>
<span class="token punctuation">&#125;</span>


<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
    slice <span class="token operator">:=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">int</span><span class="token punctuation">&#123;</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">&#125;</span>
    index <span class="token operator">:=</span> <span class="token number">2</span>
    slice <span class="token operator">=</span> <span class="token function">Remove</span><span class="token punctuation">(</span>slice<span class="token punctuation">,</span> index<span class="token punctuation">)</span>
    fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>slice<span class="token punctuation">)</span>

<span class="token punctuation">&#125;</span>
<span class="token comment">// 在上述代码中，Remove函数的参数包括一个整型切片“slice”和一个索引值“index”，表示需要删除的元素在切片中的位置。接着，我们使用“append”函数的特性来实现删除操作：首先将“slice”切片中的前半部分（从0到“index”-1）和后半部分（从“index”+1到切片的末尾）连接起来，返回一个新的切片作为最终结果。通过将这个新的切片赋值给“slice”，就实现了删除操作。</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>####### 追加<br>append：在切片的尾部追加元素，长度加1</p>
<p>增加元素后，有可能超过当前容量，导致切片扩容。</p>
<h4 id="长度和容量-1"><a href="#长度和容量-1" class="headerlink" title="长度和容量"></a><strong>长度和容量</strong></h4><pre class="line-numbers language-go" data-language="go"><code class="language-go">s1 <span class="token operator">:=</span> <span class="token function">make</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">int</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span>
fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"s1 %p %p len=%d cap=%d  %v\n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>s1<span class="token punctuation">,</span> <span class="token operator">&amp;</span>s1<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>s1<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cap</span><span class="token punctuation">(</span>s1<span class="token punctuation">)</span><span class="token punctuation">,</span> s1<span class="token punctuation">)</span>
s2 <span class="token operator">:=</span> <span class="token function">append</span><span class="token punctuation">(</span>s1<span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span>
fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"s2 %p %p len=%d cap=%d  %v\n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>s2<span class="token punctuation">,</span> <span class="token operator">&amp;</span>s2<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>s2<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cap</span><span class="token punctuation">(</span>s2<span class="token punctuation">)</span><span class="token punctuation">,</span> s2<span class="token punctuation">)</span>
fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"s1 %p %p len=%d cap=%d  %v\n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>s1<span class="token punctuation">,</span> <span class="token operator">&amp;</span>s1<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>s1<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cap</span><span class="token punctuation">(</span>s1<span class="token punctuation">)</span><span class="token punctuation">,</span> s1<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<pre class="line-numbers language-go" data-language="go"><code class="language-go">目前还没有超过容量，底层共用一个数组，但是，对底层数组使用的片段不一样
s1 <span class="token number">0x1400000c030</span> <span class="token number">0x140000141b0</span> <span class="token builtin">len</span><span class="token operator">=</span><span class="token number">3</span> <span class="token builtin">cap</span><span class="token operator">=</span><span class="token number">5</span>  <span class="token punctuation">[</span><span class="token number">0</span> <span class="token number">0</span> <span class="token number">0</span><span class="token punctuation">]</span>
s2 <span class="token number">0x1400000c060</span> <span class="token number">0x140000141b0</span> <span class="token builtin">len</span><span class="token operator">=</span><span class="token number">4</span> <span class="token builtin">cap</span><span class="token operator">=</span><span class="token number">5</span>  <span class="token punctuation">[</span><span class="token number">0</span> <span class="token number">0</span> <span class="token number">0</span> <span class="token number">4</span><span class="token punctuation">]</span>
s1 <span class="token number">0x1400000c030</span> <span class="token number">0x140000141b0</span> <span class="token builtin">len</span><span class="token operator">=</span><span class="token number">3</span> <span class="token builtin">cap</span><span class="token operator">=</span><span class="token number">5</span>  <span class="token punctuation">[</span><span class="token number">0</span> <span class="token number">0</span> <span class="token number">0</span><span class="token punctuation">]</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>


<pre class="line-numbers language-go" data-language="go"><code class="language-go">	s1 <span class="token operator">:=</span> <span class="token function">make</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">int</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"s1 %p %p len=%d cap=%d  %v\n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>s1<span class="token punctuation">,</span> <span class="token operator">&amp;</span>s1<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>s1<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cap</span><span class="token punctuation">(</span>s1<span class="token punctuation">)</span><span class="token punctuation">,</span> s1<span class="token punctuation">)</span>
	s2 <span class="token operator">:=</span> <span class="token function">append</span><span class="token punctuation">(</span>s1<span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"s2 %p %p len=%d cap=%d  %v\n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>s2<span class="token punctuation">,</span> <span class="token operator">&amp;</span>s2<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>s2<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cap</span><span class="token punctuation">(</span>s2<span class="token punctuation">)</span><span class="token punctuation">,</span> s2<span class="token punctuation">)</span>
	s3 <span class="token operator">:=</span> <span class="token function">append</span><span class="token punctuation">(</span>s1<span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span>
        <span class="token comment">// 因为没有超过最大容量，所以还是共用底层数组，那么s3基于s1增加元素就是在[0,0,0]上增加-1，底层数组的值就为[0,0,0,-1]</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"s3 %p %p len=%d cap=%d  %v\n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>s3<span class="token punctuation">,</span> <span class="token operator">&amp;</span>s3<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>s3<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cap</span><span class="token punctuation">(</span>s3<span class="token punctuation">)</span><span class="token punctuation">,</span> s3<span class="token punctuation">)</span>
        <span class="token comment">// 因为是共用底层数组所以上面的第四个元素值为-1,所以s2得第四个元素也被修改成-1</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"s2 %p %p len=%d cap=%d  %v\n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>s2<span class="token punctuation">,</span> <span class="token operator">&amp;</span>s2<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>s2<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cap</span><span class="token punctuation">(</span>s2<span class="token punctuation">)</span><span class="token punctuation">,</span> s2<span class="token punctuation">)</span>
<span class="token comment">// 输出</span>
s1 <span class="token number">0x1400000c030</span> <span class="token number">0x140000141b0</span> <span class="token builtin">len</span><span class="token operator">=</span><span class="token number">3</span> <span class="token builtin">cap</span><span class="token operator">=</span><span class="token number">5</span>  <span class="token punctuation">[</span><span class="token number">0</span> <span class="token number">0</span> <span class="token number">0</span><span class="token punctuation">]</span>
s2 <span class="token number">0x1400000c060</span> <span class="token number">0x140000141b0</span> <span class="token builtin">len</span><span class="token operator">=</span><span class="token number">4</span> <span class="token builtin">cap</span><span class="token operator">=</span><span class="token number">5</span>  <span class="token punctuation">[</span><span class="token number">0</span> <span class="token number">0</span> <span class="token number">0</span> <span class="token number">4</span><span class="token punctuation">]</span>
s3 <span class="token number">0x1400000c090</span> <span class="token number">0x140000141b0</span> <span class="token builtin">len</span><span class="token operator">=</span><span class="token number">4</span> <span class="token builtin">cap</span><span class="token operator">=</span><span class="token number">5</span>  <span class="token punctuation">[</span><span class="token number">0</span> <span class="token number">0</span> <span class="token number">0</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span>
s2 <span class="token number">0x1400000c060</span> <span class="token number">0x140000141b0</span> <span class="token builtin">len</span><span class="token operator">=</span><span class="token number">4</span> <span class="token builtin">cap</span><span class="token operator">=</span><span class="token number">5</span>  <span class="token punctuation">[</span><span class="token number">0</span> <span class="token number">0</span> <span class="token number">0</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>




<pre class="line-numbers language-go" data-language="go"><code class="language-go">	s1 <span class="token operator">:=</span> <span class="token function">make</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">int</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"s1 %p %p len=%d cap=%d  %v\n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>s1<span class="token punctuation">,</span> <span class="token operator">&amp;</span>s1<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>s1<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cap</span><span class="token punctuation">(</span>s1<span class="token punctuation">)</span><span class="token punctuation">,</span> s1<span class="token punctuation">)</span>
	s2 <span class="token operator">:=</span> <span class="token function">append</span><span class="token punctuation">(</span>s1<span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"s2 %p %p len=%d cap=%d  %v\n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>s2<span class="token punctuation">,</span> <span class="token operator">&amp;</span>s2<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>s2<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cap</span><span class="token punctuation">(</span>s2<span class="token punctuation">)</span><span class="token punctuation">,</span> s2<span class="token punctuation">)</span>
	s3 <span class="token operator">:=</span> <span class="token function">append</span><span class="token punctuation">(</span>s1<span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"s3 %p %p len=%d cap=%d  %v\n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>s3<span class="token punctuation">,</span> <span class="token operator">&amp;</span>s3<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>s3<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cap</span><span class="token punctuation">(</span>s3<span class="token punctuation">)</span><span class="token punctuation">,</span> s3<span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"s2 %p %p len=%d cap=%d  %v\n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>s2<span class="token punctuation">,</span> <span class="token operator">&amp;</span>s2<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>s2<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cap</span><span class="token punctuation">(</span>s2<span class="token punctuation">)</span><span class="token punctuation">,</span> s2<span class="token punctuation">)</span>
	s4 <span class="token operator">:=</span> <span class="token function">append</span><span class="token punctuation">(</span>s3<span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"s4 %p %p len=%d cap=%d  %v\n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>s4<span class="token punctuation">,</span> <span class="token operator">&amp;</span>s4<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>s4<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cap</span><span class="token punctuation">(</span>s4<span class="token punctuation">)</span><span class="token punctuation">,</span> s4<span class="token punctuation">)</span>
<span class="token comment">// 输出 可以看到，底层数组的地址经过扩容之后地址已经改变，说明已经是一个新的底层数组了</span>
s1 切片地址：<span class="token number">0x14000122018</span> 底层数组地址：<span class="token number">0x14000130030</span> <span class="token builtin">len</span><span class="token operator">=</span><span class="token number">3</span> <span class="token builtin">cap</span><span class="token operator">=</span><span class="token number">5</span>  <span class="token punctuation">[</span><span class="token number">0</span> <span class="token number">0</span> <span class="token number">0</span><span class="token punctuation">]</span>
s2 切片地址：<span class="token number">0x14000122048</span> 底层数组地址：<span class="token number">0x14000130030</span> <span class="token builtin">len</span><span class="token operator">=</span><span class="token number">4</span> <span class="token builtin">cap</span><span class="token operator">=</span><span class="token number">5</span>  <span class="token punctuation">[</span><span class="token number">0</span> <span class="token number">0</span> <span class="token number">0</span> <span class="token number">4</span><span class="token punctuation">]</span>
s3 切片地址：<span class="token number">0x14000122078</span> 底层数组地址：<span class="token number">0x14000130030</span> <span class="token builtin">len</span><span class="token operator">=</span><span class="token number">4</span> <span class="token builtin">cap</span><span class="token operator">=</span><span class="token number">5</span>  <span class="token punctuation">[</span><span class="token number">0</span> <span class="token number">0</span> <span class="token number">0</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span>
s2 切片地址：<span class="token number">0x14000122048</span> 底层数组地址：<span class="token number">0x14000130030</span> <span class="token builtin">len</span><span class="token operator">=</span><span class="token number">4</span> <span class="token builtin">cap</span><span class="token operator">=</span><span class="token number">5</span>  <span class="token punctuation">[</span><span class="token number">0</span> <span class="token number">0</span> <span class="token number">0</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span>
s4 切片地址：<span class="token number">0x140001220c0</span> 底层数组地址：<span class="token number">0x14000138000</span> <span class="token builtin">len</span><span class="token operator">=</span><span class="token number">7</span> <span class="token builtin">cap</span><span class="token operator">=</span><span class="token number">10</span>  <span class="token punctuation">[</span><span class="token number">0</span> <span class="token number">0</span> <span class="token number">0</span> <span class="token operator">-</span><span class="token number">1</span> <span class="token number">6</span> <span class="token number">7</span> <span class="token number">8</span><span class="token punctuation">]</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<ul>
<li>append一定返回一个新的切片</li>
<li>append可以增加若干元素<ul>
<li>如果增加元素时，当前长度+新增个数 &lt;&#x3D;cap 则不扩容<ul>
<li>原切片使用原来的底层数组，返回的新切片也使用这个底层数组</li>
<li>返回的新切片有新的长度</li>
<li>原切片长度不变</li>
</ul>
</li>
<li>如果增加元素时，当前长度+新增个数 &gt; cap 则需要扩容底层数组<ul>
<li>生成新的底层数组，新生成的切片使用新数组，将旧元素复制到新数组，在其后增加新元素</li>
<li>原切片底层数组、长度、容量不变</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>####### 底层库容策略<br><a target="_blank" rel="noopener" href="https://go.dev/src/runtime/slice.go">https://go.dev/src/runtime/slice.go</a></p>
<p>（老版本）实际上，当扩容后的cap&lt;1024时，扩容是以翻倍的形式，容量变成之前的2倍，如之前容量是10，扩容后容量变成20；当cap&gt;&#x3D;1024时，变成之前的1.25倍，如之前容量是10，扩容后变成12.5</p>
<p>（新版本1.18+） 阈值变成了256，当扩容后的cap&lt;256时，扩容翻倍，容量变成之前的2倍；当cap&gt;&#x3D;256时，<code>newcap += (newcap + 3*threshold) / 4 计算后就是 newcap = newcap +newcap/4 + 192 </code>，即1.25倍后再加192。</p>
<p>扩容是创建新的内部数组，把原内存数据拷贝到新内存空间，然后在新内存空间上执行元素追加操作。</p>
<p>切片频繁库容成本非常高，所以尽量估算出使用的大小，一次性给够，建议用make，常用<code>make([]int,0,100)</code> </p>
<h5 id="引用类型"><a href="#引用类型" class="headerlink" title="引用类型"></a><strong>引用类型</strong></h5><pre class="line-numbers language-go" data-language="go"><code class="language-go"><span class="token keyword">package</span> main
<span class="token keyword">import</span> <span class="token punctuation">(</span>
 <span class="token string">"fmt"</span>
<span class="token punctuation">)</span>
<span class="token keyword">func</span> <span class="token function">showAddr</span><span class="token punctuation">(</span>s <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">int</span> <span class="token punctuation">&#123;</span>
 fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"s %p, %p, %d, %d, %v\n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>s<span class="token punctuation">,</span> <span class="token operator">&amp;</span>s<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cap</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">,</span> s<span class="token punctuation">)</span>
 <span class="token comment">// 修改一个元素</span>
 <span class="token keyword">if</span> <span class="token function">len</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">0</span> <span class="token punctuation">&#123;</span>
 s<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">123</span>
 <span class="token punctuation">&#125;</span>
 <span class="token keyword">return</span> s <span class="token punctuation">&#125;</span>
<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
 s1 <span class="token operator">:=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">int</span><span class="token punctuation">&#123;</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">,</span> <span class="token number">33</span><span class="token punctuation">&#125;</span>
 fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"s1 %p, %p, %d, %d, %v\n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>s1<span class="token punctuation">,</span> <span class="token operator">&amp;</span>s1<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>s1<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cap</span><span class="token punctuation">(</span>s1<span class="token punctuation">)</span><span class="token punctuation">,</span> s1<span class="token punctuation">)</span>
 s2 <span class="token operator">:=</span> s1
 fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"s2 %p, %p, %d, %d, %v\n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>s2<span class="token punctuation">,</span> <span class="token operator">&amp;</span>s2<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>s2<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cap</span><span class="token punctuation">(</span>s2<span class="token punctuation">)</span><span class="token punctuation">,</span> s2<span class="token punctuation">)</span>
 fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">"~~~~~~~~~~~~~~~~~~~~~~~~~~~"</span><span class="token punctuation">)</span>
 s3 <span class="token operator">:=</span> <span class="token function">showAddr</span><span class="token punctuation">(</span>s1<span class="token punctuation">)</span>
 fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"s1 %p, %p, %d, %d, %v\n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>s1<span class="token punctuation">,</span> <span class="token operator">&amp;</span>s1<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>s1<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cap</span><span class="token punctuation">(</span>s1<span class="token punctuation">)</span><span class="token punctuation">,</span> s1<span class="token punctuation">)</span>
 fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"s2 %p, %p, %d, %d, %v\n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>s2<span class="token punctuation">,</span> <span class="token operator">&amp;</span>s2<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>s2<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cap</span><span class="token punctuation">(</span>s2<span class="token punctuation">)</span><span class="token punctuation">,</span> s2<span class="token punctuation">)</span>
 fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"s3 %p, %p, %d, %d, %v\n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>s3<span class="token punctuation">,</span> <span class="token operator">&amp;</span>s3<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>s3<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cap</span><span class="token punctuation">(</span>s3<span class="token punctuation">)</span><span class="token punctuation">,</span> s3<span class="token punctuation">)</span> <span class="token punctuation">&#125;</span>
运行结果
s1 <span class="token number">0xc000008078</span><span class="token punctuation">,</span> <span class="token number">0xc0000101b0</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">10</span> <span class="token number">20</span> <span class="token number">33</span><span class="token punctuation">]</span>
s2 <span class="token number">0xc0000080a8</span><span class="token punctuation">,</span> <span class="token number">0xc0000101b0</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">10</span> <span class="token number">20</span> <span class="token number">33</span><span class="token punctuation">]</span>
~~~~~~~~~~~~~~~~~~~~~~~~~~~
s  <span class="token number">0xc0000080f0</span><span class="token punctuation">,</span> <span class="token number">0xc0000101b0</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">10</span> <span class="token number">20</span> <span class="token number">33</span><span class="token punctuation">]</span>
s1 <span class="token number">0xc000008078</span><span class="token punctuation">,</span> <span class="token number">0xc0000101b0</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">123</span> <span class="token number">20</span> <span class="token number">33</span><span class="token punctuation">]</span>
s2 <span class="token number">0xc0000080a8</span><span class="token punctuation">,</span> <span class="token number">0xc0000101b0</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">123</span> <span class="token number">20</span> <span class="token number">33</span><span class="token punctuation">]</span>
s3 <span class="token number">0xc0000080d8</span><span class="token punctuation">,</span> <span class="token number">0xc0000101b0</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">123</span> <span class="token number">20</span> <span class="token number">33</span><span class="token punctuation">]</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>这说明，底层数组是同一份，修改切片中某个已有元素，那么所有切片都能看到。</p>
<p>那如果在上面showAddr函数中对切片增加一个元素会怎么样？</p>
<pre class="line-numbers language-go" data-language="go"><code class="language-go"><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token punctuation">(</span>
	<span class="token string">"fmt"</span>
<span class="token punctuation">)</span>

<span class="token keyword">func</span> <span class="token function">showAddr</span><span class="token punctuation">(</span>arr <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">int</span> <span class="token punctuation">&#123;</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"arr 切片地址：%p 底层数组地址：%p len=%d cap=%d  %v\n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>arr<span class="token punctuation">,</span> <span class="token operator">&amp;</span>arr<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cap</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span><span class="token punctuation">,</span> arr<span class="token punctuation">)</span>
	arr <span class="token operator">=</span> <span class="token function">append</span><span class="token punctuation">(</span>arr<span class="token punctuation">,</span> <span class="token number">444</span><span class="token punctuation">,</span> <span class="token number">222</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"arr2 切片地址：%p 底层数组地址：%p len=%d cap=%d  %v\n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>arr<span class="token punctuation">,</span> <span class="token operator">&amp;</span>arr<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cap</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span><span class="token punctuation">,</span> arr<span class="token punctuation">)</span>

	<span class="token comment">//if len(arr) > 0 &#123;</span>
	<span class="token comment">//	arr[0] = 22222</span>
	<span class="token comment">//&#125;</span>
	<span class="token keyword">return</span> arr
<span class="token punctuation">&#125;</span>
<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
	<span class="token comment">//a := []int&#123;1, 3, 5, 6&#125;</span>
	<span class="token comment">//fmt.Printf("%v, %p  %p", a, &amp;a, &amp;a[0])</span>
	s1 <span class="token operator">:=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">int</span><span class="token punctuation">&#123;</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">,</span> <span class="token number">33</span><span class="token punctuation">&#125;</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"s1 切片地址：%p 底层数组地址：%p len=%d cap=%d  %v\n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>s1<span class="token punctuation">,</span> <span class="token operator">&amp;</span>s1<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>s1<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cap</span><span class="token punctuation">(</span>s1<span class="token punctuation">)</span><span class="token punctuation">,</span> s1<span class="token punctuation">)</span>
	s2 <span class="token operator">:=</span> s1
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"s2 切片地址：%p 底层数组地址：%p len=%d cap=%d  %v\n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>s2<span class="token punctuation">,</span> <span class="token operator">&amp;</span>s2<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>s2<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cap</span><span class="token punctuation">(</span>s2<span class="token punctuation">)</span><span class="token punctuation">,</span> s2<span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">"~~~~~~~~~~~~~~~~"</span><span class="token punctuation">)</span>
	s3 <span class="token operator">:=</span> <span class="token function">showAddr</span><span class="token punctuation">(</span>s1<span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"s1 切片地址：%p 底层数组地址：%p len=%d cap=%d  %v\n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>s1<span class="token punctuation">,</span> <span class="token operator">&amp;</span>s1<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>s1<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cap</span><span class="token punctuation">(</span>s1<span class="token punctuation">)</span><span class="token punctuation">,</span> s1<span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"s2 切片地址：%p 底层数组地址：%p len=%d cap=%d  %v\n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>s2<span class="token punctuation">,</span> <span class="token operator">&amp;</span>s2<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>s2<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cap</span><span class="token punctuation">(</span>s2<span class="token punctuation">)</span><span class="token punctuation">,</span> s2<span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"s3 切片地址：%p 底层数组地址：%p len=%d cap=%d  %v\n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>s3<span class="token punctuation">,</span> <span class="token operator">&amp;</span>s3<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>s3<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cap</span><span class="token punctuation">(</span>s3<span class="token punctuation">)</span><span class="token punctuation">,</span> s3<span class="token punctuation">)</span>
<span class="token punctuation">&#125;</span>
<span class="token comment">// 输出</span>
s1 切片地址：<span class="token number">0x14000116018</span> 底层数组地址：<span class="token number">0x1400012a018</span> <span class="token builtin">len</span><span class="token operator">=</span><span class="token number">3</span> <span class="token builtin">cap</span><span class="token operator">=</span><span class="token number">3</span>  <span class="token punctuation">[</span><span class="token number">10</span> <span class="token number">20</span> <span class="token number">33</span><span class="token punctuation">]</span>
s2 切片地址：<span class="token number">0x14000116048</span> 底层数组地址：<span class="token number">0x1400012a018</span> <span class="token builtin">len</span><span class="token operator">=</span><span class="token number">3</span> <span class="token builtin">cap</span><span class="token operator">=</span><span class="token number">3</span>  <span class="token punctuation">[</span><span class="token number">10</span> <span class="token number">20</span> <span class="token number">33</span><span class="token punctuation">]</span>
~~~~~~~~~~~~~~~~
arr 切片地址：<span class="token number">0x14000116090</span> 底层数组地址：<span class="token number">0x1400012a018</span> <span class="token builtin">len</span><span class="token operator">=</span><span class="token number">3</span> <span class="token builtin">cap</span><span class="token operator">=</span><span class="token number">3</span>  <span class="token punctuation">[</span><span class="token number">10</span> <span class="token number">20</span> <span class="token number">33</span><span class="token punctuation">]</span>
arr2 切片地址：<span class="token number">0x14000116090</span> 底层数组地址：<span class="token number">0x14000126060</span> <span class="token builtin">len</span><span class="token operator">=</span><span class="token number">5</span> <span class="token builtin">cap</span><span class="token operator">=</span><span class="token number">6</span>  <span class="token punctuation">[</span><span class="token number">10</span> <span class="token number">20</span> <span class="token number">33</span> <span class="token number">444</span> <span class="token number">222</span><span class="token punctuation">]</span>
s1 切片地址：<span class="token number">0x14000116018</span> 底层数组地址：<span class="token number">0x1400012a018</span> <span class="token builtin">len</span><span class="token operator">=</span><span class="token number">3</span> <span class="token builtin">cap</span><span class="token operator">=</span><span class="token number">3</span>  <span class="token punctuation">[</span><span class="token number">10</span> <span class="token number">20</span> <span class="token number">33</span><span class="token punctuation">]</span>
s2 切片地址：<span class="token number">0x14000116048</span> 底层数组地址：<span class="token number">0x1400012a018</span> <span class="token builtin">len</span><span class="token operator">=</span><span class="token number">3</span> <span class="token builtin">cap</span><span class="token operator">=</span><span class="token number">3</span>  <span class="token punctuation">[</span><span class="token number">10</span> <span class="token number">20</span> <span class="token number">33</span><span class="token punctuation">]</span>
s3 切片地址：<span class="token number">0x14000116078</span> 底层数组地址：<span class="token number">0x14000126060</span> <span class="token builtin">len</span><span class="token operator">=</span><span class="token number">5</span> <span class="token builtin">cap</span><span class="token operator">=</span><span class="token number">6</span>  <span class="token punctuation">[</span><span class="token number">10</span> <span class="token number">20</span> <span class="token number">33</span> <span class="token number">444</span> <span class="token number">222</span><span class="token punctuation">]</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>


<p>可以看到showAddr传入s1，但是返回的s3已经和s1不共用一个底层数组了。</p>
<p>其实这里还是值拷贝，不过拷贝的是切片的标头值(Header)。标头值内指针也被复制，刚复制完大家指向同一个底层数组罢了，但是一旦操作切片时扩容了，或另一个切片增加元素，那么就不能简单归结为“切片”是引用类型，拷贝了地址“这样来解释”</p>
<p><strong>GO语言中全部都是值传递，整型、数组这样的类型的值是完全复制，slice、map、channel、interface、function这样的引用类型也是值拷贝，不过复制的是标头值。</strong></p>
<h5 id="截取子切片"><a href="#截取子切片" class="headerlink" title="截取子切片"></a>截取子切片</h5><p>切片可以通过指定索引区间获得一个子切片，格式为slice[start:end]，规则就是前包后不包</p>
<pre class="line-numbers language-go" data-language="go"><code class="language-go"><span class="token keyword">package</span> main
<span class="token keyword">import</span> <span class="token punctuation">(</span>
 <span class="token string">"fmt"</span>
<span class="token punctuation">)</span>
<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
 s1 <span class="token operator">:=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">int</span><span class="token punctuation">&#123;</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">30</span><span class="token punctuation">,</span> <span class="token number">50</span><span class="token punctuation">,</span> <span class="token number">70</span><span class="token punctuation">,</span> <span class="token number">90</span><span class="token punctuation">&#125;</span> <span class="token comment">// 容量、长度为5，索引0、1、2、3、4</span>
 <span class="token keyword">for</span> i <span class="token operator">:=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token function">len</span><span class="token punctuation">(</span>s1<span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span> <span class="token punctuation">&#123;</span>
 fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"%d : addr = %p\n"</span><span class="token punctuation">,</span> i<span class="token punctuation">,</span> <span class="token operator">&amp;</span>s1<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>
 <span class="token punctuation">&#125;</span>
 fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"s1 %p, %p, l=%-2d, c=%-2d, %v\n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>s1<span class="token punctuation">,</span> <span class="token operator">&amp;</span>s1<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>s1<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cap</span><span class="token punctuation">(</span>s1<span class="token punctuation">)</span><span class="token punctuation">,</span> s1<span class="token punctuation">)</span>
 s2 <span class="token operator">:=</span> s1 <span class="token comment">// 和s1共用底层数组  [10,30,50,70,90] </span>
 fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"s2 %p, %p, l=%-2d, c=%-2d, %v\n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>s2<span class="token punctuation">,</span> <span class="token operator">&amp;</span>s2<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>s2<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cap</span><span class="token punctuation">(</span>s2<span class="token punctuation">)</span><span class="token punctuation">,</span> s2<span class="token punctuation">)</span>
 s3 <span class="token operator">:=</span> s1<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">]</span> <span class="token comment">// 和s1共用底层数组，从头到尾元素都要</span>
 fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"s3 %p, %p, l=%-2d, c=%-2d, %v\n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>s3<span class="token punctuation">,</span> <span class="token operator">&amp;</span>s3<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>s3<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cap</span><span class="token punctuation">(</span>s3<span class="token punctuation">)</span><span class="token punctuation">,</span> s3<span class="token punctuation">)</span>
 fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">"~~~~~~~~~~~~~~~~~~~~~~~~~~~"</span><span class="token punctuation">)</span>
 s4 <span class="token operator">:=</span> s1<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token punctuation">]</span> <span class="token comment">// 掐头，容量、长度都为4，首地址偏移1个元素，共用底层数组,[30,50,70,90]</span>
 fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"s4 %p, %p, l=%-2d, c=%-2d, %v\n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>s4<span class="token punctuation">,</span> <span class="token operator">&amp;</span>s4<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>s4<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cap</span><span class="token punctuation">(</span>s4<span class="token punctuation">)</span><span class="token punctuation">,</span> s4<span class="token punctuation">)</span>
 s5 <span class="token operator">:=</span> s1<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token number">4</span><span class="token punctuation">]</span> <span class="token comment">// 掐头去尾，容量为4，长度为3，首地址偏移1个元素，共用底层数组,[30,50,70]</span>
 fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"s5 %p, %p, l=%-2d, c=%-2d, %v\n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>s5<span class="token punctuation">,</span> <span class="token operator">&amp;</span>s5<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>s5<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cap</span><span class="token punctuation">(</span>s5<span class="token punctuation">)</span><span class="token punctuation">,</span> s5<span class="token punctuation">)</span>
 s6 <span class="token operator">:=</span> s1<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token number">4</span><span class="token punctuation">]</span> <span class="token comment">// 去尾，容量为5，长度为4，首地址不变，共用底层数组 [10,30,50,70]</span>
 fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"s6 %p, %p, l=%-2d, c=%-2d, %v\n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>s6<span class="token punctuation">,</span> <span class="token operator">&amp;</span>s6<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>s6<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cap</span><span class="token punctuation">(</span>s6<span class="token punctuation">)</span><span class="token punctuation">,</span> s6<span class="token punctuation">)</span>
 fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">"~~~~~~~~~~~~~~~~~~~~~~~~~~~"</span><span class="token punctuation">)</span>
 s7 <span class="token operator">:=</span> s1<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token comment">// 首地址偏移1个元素，长度为0，容量为4，共用底层数组  []</span>
 <span class="token comment">// fmt.Printf("s7 %p, %p, l=%-2d, c=%-2d, %v\n", &amp;s7, &amp;s7[0], len(s7), cap(s7), s7) // 由于长度为0，所以不能s7[0]报错</span>
 fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"s7 %p, l=%-2d, c=%-2d, %v\n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>s7<span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>s7<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cap</span><span class="token punctuation">(</span>s7<span class="token punctuation">)</span><span class="token punctuation">,</span> s7<span class="token punctuation">)</span>
 <span class="token comment">// s7 = append(s7, 111) // 请问，为s7增加一个元素，s1、s7分别是什么？ </span>
 <span class="token comment">// fmt.Printf("s1 %p, %p, l=%-2d, c=%-2d, %v\n", &amp;s1, &amp;s1[0], len(s1), cap(s1), s1) [10,111,50,70,90]</span>
 <span class="token comment">// fmt.Printf("s7 %p, %p, l=%-2d, c=%-2d, %v\n", &amp;s7, &amp;s7[0], len(s7), cap(s7), s7) [111]</span>
 fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">"~~~~~~~~~~~~~~~~~~~~~~~~~~~"</span><span class="token punctuation">)</span>
 s8 <span class="token operator">:=</span> s1<span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">:</span><span class="token number">4</span><span class="token punctuation">]</span> <span class="token comment">// 首地址偏移4个元素，长度为0，容量为1，因为最后一个元素没在切片中，共用底层数组  []</span>
 fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"s8 %p, l=%-2d, c=%-2d, %v\n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>s8<span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>s8<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cap</span><span class="token punctuation">(</span>s8<span class="token punctuation">)</span><span class="token punctuation">,</span> s8<span class="token punctuation">)</span>
 s9 <span class="token operator">:=</span> s1<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">:</span><span class="token number">5</span><span class="token punctuation">]</span> <span class="token comment">// 首地址偏移5个元素，长度为0，容量为0，共用底层数组</span>
 fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"s9 %p, l=%-2d, c=%-2d, %v\n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>s9<span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>s9<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cap</span><span class="token punctuation">(</span>s9<span class="token punctuation">)</span><span class="token punctuation">,</span> s9<span class="token punctuation">)</span> <span class="token comment">// []</span>
 fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">"~~~~~~~~~~~~~~~~~~~~~~~~~~~"</span><span class="token punctuation">)</span>
 s9 <span class="token operator">=</span> <span class="token function">append</span><span class="token punctuation">(</span>s9<span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">)</span> <span class="token comment">// 增加元素会怎么样？s1、s9分别是什么？</span>
 fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"s1 %p, %p, l=%-2d, c=%-2d, %v\n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>s1<span class="token punctuation">,</span> <span class="token operator">&amp;</span>s1<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>s1<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cap</span><span class="token punctuation">(</span>s1<span class="token punctuation">)</span><span class="token punctuation">,</span> s1<span class="token punctuation">)</span>  <span class="token comment">//[10,111,50,70,90]</span>
 fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"s9 %p, %p, l=%-2d, c=%-2d, %v\n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>s9<span class="token punctuation">,</span> <span class="token operator">&amp;</span>s9<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>s9<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cap</span><span class="token punctuation">(</span>s9<span class="token punctuation">)</span><span class="token punctuation">,</span> s9<span class="token punctuation">)</span> <span class="token punctuation">&#125;</span> <span class="token comment">//[11]  这个是新数组</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>上面例子可以看出，切这个操作都是从同一个底层数组上取的段，所以子切片和原始切片共用同一个底层数组</p>
<ul>
<li>start默认为0，end默认为len(slice)即切片长度</li>
<li>通过指针确定底层数组从哪里开始共享</li>
<li>长度为end-start</li>
<li>容量是底层数组从便偏移的元素到结尾能容纳几个元素</li>
</ul>
<p><img src="/images/IYn3mikXQ-6q5ivRZQxaGgGu_s0wXPj4nrWK23IzVpU.png" alt="image"></p>
<p><strong>子切片操作不可能构造出一个新的底层数组！ 子切片本质上利用原来的切片的部分元素，共用一个底层数组，因此不会导致扩容。</strong></p>
<p>数组也可以切片，会生成新的切片。</p>
<pre class="line-numbers language-go" data-language="go"><code class="language-go"><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token string">"fmt"</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
	s1 <span class="token operator">:=</span> <span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token builtin">int</span><span class="token punctuation">&#123;</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">30</span><span class="token punctuation">,</span> <span class="token number">50</span><span class="token punctuation">,</span> <span class="token number">70</span><span class="token punctuation">,</span> <span class="token number">90</span><span class="token punctuation">&#125;</span>
	<span class="token keyword">for</span> i <span class="token operator">:=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token function">len</span><span class="token punctuation">(</span>s1<span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span> <span class="token punctuation">&#123;</span>
		fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"%d : addr = %p\n"</span><span class="token punctuation">,</span> i<span class="token punctuation">,</span> <span class="token operator">&amp;</span>s1<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>
	<span class="token punctuation">&#125;</span>
	s4 <span class="token operator">:=</span> s1<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token punctuation">]</span> <span class="token comment">// 掐头，容量、长度都为4，首地址偏移1个元素，共用底层数组</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"s4 %p, %p, l=%-2d, c=%-2d, %v\n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>s4<span class="token punctuation">,</span> <span class="token operator">&amp;</span>s4<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>s4<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cap</span><span class="token punctuation">(</span>s4<span class="token punctuation">)</span><span class="token punctuation">,</span> s4<span class="token punctuation">)</span>
	s4<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">100</span> <span class="token comment">// s1、s4分别是什么？</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"s1 %p, %p, l=%-2d, c=%-2d, %v\n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>s1<span class="token punctuation">,</span> <span class="token operator">&amp;</span>s1<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>s1<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cap</span><span class="token punctuation">(</span>s1<span class="token punctuation">)</span><span class="token punctuation">,</span> s1<span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"s4 %p, %p, l=%-2d, c=%-2d, %v\n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>s4<span class="token punctuation">,</span> <span class="token operator">&amp;</span>s4<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>s4<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cap</span><span class="token punctuation">(</span>s4<span class="token punctuation">)</span><span class="token punctuation">,</span> s4<span class="token punctuation">)</span>

	s4 <span class="token operator">=</span> <span class="token function">append</span><span class="token punctuation">(</span>s4<span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">,</span> <span class="token number">22</span><span class="token punctuation">)</span> <span class="token comment">// 是否扩容？会怎样？</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"s1 %p, %p, l=%-2d, c=%-2d, %v\n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>s1<span class="token punctuation">,</span> <span class="token operator">&amp;</span>s1<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>s1<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cap</span><span class="token punctuation">(</span>s1<span class="token punctuation">)</span><span class="token punctuation">,</span> s1<span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"s4 %p, %p, l=%-2d, c=%-2d, %v\n"</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>s4<span class="token punctuation">,</span> <span class="token operator">&amp;</span>s4<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>s4<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cap</span><span class="token punctuation">(</span>s4<span class="token punctuation">)</span><span class="token punctuation">,</span> s4<span class="token punctuation">)</span>
<span class="token punctuation">&#125;</span>
<span class="token comment">// 输出</span>
<span class="token number">0</span> <span class="token punctuation">:</span> addr <span class="token operator">=</span> <span class="token number">0x140000ae030</span>
<span class="token number">1</span> <span class="token punctuation">:</span> addr <span class="token operator">=</span> <span class="token number">0x140000ae038</span>
<span class="token number">2</span> <span class="token punctuation">:</span> addr <span class="token operator">=</span> <span class="token number">0x140000ae040</span>
<span class="token number">3</span> <span class="token punctuation">:</span> addr <span class="token operator">=</span> <span class="token number">0x140000ae048</span>
<span class="token number">4</span> <span class="token punctuation">:</span> addr <span class="token operator">=</span> <span class="token number">0x140000ae050</span>
s4 <span class="token number">0x140000a0018</span><span class="token punctuation">,</span> <span class="token number">0x140000ae038</span><span class="token punctuation">,</span> l<span class="token operator">=</span><span class="token number">4</span> <span class="token punctuation">,</span> c<span class="token operator">=</span><span class="token number">4</span> <span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">30</span> <span class="token number">50</span> <span class="token number">70</span> <span class="token number">90</span><span class="token punctuation">]</span>
s1 <span class="token number">0x140000ae030</span><span class="token punctuation">,</span> <span class="token number">0x140000ae030</span><span class="token punctuation">,</span> l<span class="token operator">=</span><span class="token number">5</span> <span class="token punctuation">,</span> c<span class="token operator">=</span><span class="token number">5</span> <span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">10</span> <span class="token number">100</span> <span class="token number">50</span> <span class="token number">70</span> <span class="token number">90</span><span class="token punctuation">]</span>
s4 <span class="token number">0x140000a0018</span><span class="token punctuation">,</span> <span class="token number">0x140000ae038</span><span class="token punctuation">,</span> l<span class="token operator">=</span><span class="token number">4</span> <span class="token punctuation">,</span> c<span class="token operator">=</span><span class="token number">4</span> <span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">100</span> <span class="token number">50</span> <span class="token number">70</span> <span class="token number">90</span><span class="token punctuation">]</span>
s1 <span class="token number">0x140000ae030</span><span class="token punctuation">,</span> <span class="token number">0x140000ae030</span><span class="token punctuation">,</span> l<span class="token operator">=</span><span class="token number">5</span> <span class="token punctuation">,</span> c<span class="token operator">=</span><span class="token number">5</span> <span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">10</span> <span class="token number">100</span> <span class="token number">50</span> <span class="token number">70</span> <span class="token number">90</span><span class="token punctuation">]</span>
s4 <span class="token number">0x140000a0018</span><span class="token punctuation">,</span> <span class="token number">0x140000b0080</span><span class="token punctuation">,</span> l<span class="token operator">=</span><span class="token number">6</span> <span class="token punctuation">,</span> c<span class="token operator">=</span><span class="token number">8</span> <span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">100</span> <span class="token number">50</span> <span class="token number">70</span> <span class="token number">90</span> <span class="token number">11</span> <span class="token number">22</span><span class="token punctuation">]</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>切片总结：</p>
<ul>
<li>使用slice[start:end] 表示切片，切片长度为end-start，前包后不包</li>
<li>start缺省，表示从索引0开始</li>
<li>end缺省，表示取到末尾，包含最后一个元素，特别注意这个缺省值是<strong>len(slice) 即切片长度，不是容量</strong><ul>
<li>a1[5:] 相当于 a1[5:len(a1)]</li>
</ul>
</li>
<li>start 和end都缺省，表示从头到尾</li>
<li>start和end同时给出，要求end &gt;&#x3D;start<ul>
<li>start、end<strong>最大都不可以超过容量值cap</strong></li>
<li>假设当前容量是8，长度为5，有以下情况<ul>
<li>a1[:] 共用底层数组，从头到尾所有元素都取，相当于对标头值的拷贝，也就是指针、长度、容量都一样</li>
<li>a1[:8]，可以，end最多写成8(因为后不包)，a1[:9]不可以。该切片长度、容量都为8，这8个元素都是原序列的，一旦append就扩容</li>
<li>a1[8:]，不可以，end缺省为当前长度5，等价于a1[8:5]</li>
<li>a1[8:8]，可以，但这个切片容量和长度都为0了。注意和a1[:8]的区别</li>
<li>a1[7:7]，可以，但这个切片长度为0，容量为1</li>
<li>a1[0:0]，可以，但这个切片长度为0，容量为8</li>
<li>a1[1:5]，可以，这个切片长度为4，容量为7，相当于跳过了原序列第一个元素</li>
</ul>
</li>
</ul>
</li>
<li>切片刚产生时，和原序列（数组、切片）开始共用同一个底层数组，但是<strong>每一个切片都自己独立保存着指针、cap和len</strong></li>
<li>一旦一个切片扩容，就和原来共用一个底层数组的序列分道扬镳。</li>
</ul>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="http://dycloudnavite.love">J.</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://dycloudnavite.love/2023/02/12/Golang%E7%BA%BF%E6%80%A7%E8%A1%A8-%E6%95%B0%E7%BB%84%E3%80%81%E5%88%87%E7%89%87/">http://dycloudnavite.love/2023/02/12/Golang%E7%BA%BF%E6%80%A7%E8%A1%A8-%E6%95%B0%E7%BB%84%E3%80%81%E5%88%87%E7%89%87/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://dycloudnavite.love" target="_blank">J.のblog</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/dujie5416/tags/%E4%BA%91%E5%8E%9F%E7%94%9F/">云原生</a><a class="post-meta__tags" href="/dujie5416/tags/golang/">golang</a></div><div class="post_share"><div class="addthis_inline_share_toolbox"></div><script src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-5d034d84b42927bf" async="async"></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/dujie5416/img/wechat.jpg" target="_blank"><img class="post-qr-code-img" src="/dujie5416/img/wechat.jpg" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="/dujie5416/img/alipay.jpg" target="_blank"><img class="post-qr-code-img" src="/dujie5416/img/alipay.jpg" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/dujie5416/2023/02/12/Golang%E5%88%86%E6%94%AF%E3%80%81%E5%BE%AA%E7%8E%AF%E3%80%81%E9%9A%8F%E6%9C%BA%E6%95%B0/"><img class="prev-cover" src="/dujie5416/img/golang.png" onerror="onerror=null;src='/dujie5416/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Golang分支循环随机数</div></div></a></div><div class="next-post pull-right"><a href="/dujie5416/2023/01/13/%E3%80%90mysql%E6%95%B0%E6%8D%AE%E6%8D%9F%E5%9D%8F%EF%BC%8C%E9%80%9A%E8%BF%87ibd%E5%92%8Cfrm%E6%96%87%E4%BB%B6%E6%89%B9%E9%87%8F%E6%81%A2%E5%A4%8D%E6%95%B0%E6%8D%AE%E5%BA%93%E6%95%B0%E6%8D%AE%E3%80%91%20(1)/"><img class="next-cover" src="/dujie5416/img/mysql.jpg" onerror="onerror=null;src='/dujie5416/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">MySQL 数据损坏，通过ibd和frm文件批量恢复数据库数据</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/dujie5416/2023/05/20/Channel%E9%80%9A%E9%81%93/" title="Channel通道"><img class="cover" src="/dujie5416/img/golang.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-05-20</div><div class="title">Channel通道</div></div></a></div><div><a href="/dujie5416/2023/05/06/Golang%E4%B8%AD%E7%9A%84GORM/" title="Golang GORM使用"><img class="cover" src="/dujie5416/img/golang.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-05-06</div><div class="title">Golang GORM使用</div></div></a></div><div><a href="/dujie5416/2023/06/23/Gin%E6%A1%86%E6%9E%B6/" title="Gin框架"><img class="cover" src="/dujie5416/img/golang.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-06-23</div><div class="title">Gin框架</div></div></a></div><div><a href="/dujie5416/2023/05/15/Golang%E5%8D%8F%E7%A8%8B/" title="Golang协程"><img class="cover" src="/dujie5416/img/golang.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-05-15</div><div class="title">Golang协程</div></div></a></div><div><a href="/dujie5416/2023/04/17/Golang%E5%8F%98%E9%87%8F%E9%80%83%E9%80%B8%E2%80%94%E2%80%94%E5%A0%86%E3%80%81%E6%A0%88/" title="Golang变量逃逸————堆、栈"><img class="cover" src="/dujie5416/img/golang.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-04-17</div><div class="title">Golang变量逃逸————堆、栈</div></div></a></div><div><a href="/dujie5416/2023/02/13/Golang%E5%AD%97%E7%AC%A6%E4%B8%B2string/" title="Golang字符串"><img class="cover" src="/dujie5416/img/golang.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-02-13</div><div class="title">Golang字符串</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div id="lv-container" data-id="city" data-uid="MTAyMC81Njk5OS8zMzQ2Mw=="></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/dujie5416/img/touxiang.png" onerror="this.onerror=null;this.src='/dujie5416/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">J.</div><div class="author-info__description"></div></div><div class="card-info-data site-data is-center"><a href="/dujie5416/archives/"><div class="headline">文章</div><div class="length-num">73</div></a><a href="/dujie5416/tags/"><div class="headline">标签</div><div class="length-num">17</div></a><a href="/dujie5416/categories/"><div class="headline">分类</div><div class="length-num">11</div></a></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%BA%BF%E6%80%A7%E8%A1%A8-%E6%95%B0%E7%BB%84%E3%80%81%E5%88%87%E7%89%87"><span class="toc-number">1.</span> <span class="toc-text">线性表-数组、切片</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%80%E3%80%81%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84"><span class="toc-number">1.0.1.</span> <span class="toc-text">一、数据结构</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#1%E3%80%81%E6%95%B0%E5%80%BC%E5%A4%84%E7%90%86"><span class="toc-number">1.0.1.0.1.</span> <span class="toc-text">1、数值处理</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#%E5%8F%96%E6%95%B4"><span class="toc-number">1.0.1.0.1.1.</span> <span class="toc-text">取整</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%85%B6%E4%BB%96%E6%95%B0%E5%80%BC%E5%A4%84%E7%90%86"><span class="toc-number">1.0.1.0.2.</span> <span class="toc-text">其他数值处理</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%A0%87%E5%87%86%E8%BE%93%E5%85%A5"><span class="toc-number">1.0.1.0.3.</span> <span class="toc-text">标准输入</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BA%8C%E3%80%81%E7%BA%BF%E6%80%A7%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84"><span class="toc-number">1.0.2.</span> <span class="toc-text">二、线性数据结构</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%89%E3%80%81%E6%95%B0%E7%BB%84"><span class="toc-number">1.0.3.</span> <span class="toc-text">三、数组</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%AE%9A%E4%B9%89"><span class="toc-number">1.0.3.0.1.</span> <span class="toc-text">定义</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E9%95%BF%E5%BA%A6%E5%92%8C%E5%AE%B9%E9%87%8F"><span class="toc-number">1.0.3.0.2.</span> <span class="toc-text">长度和容量</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E7%B4%A2%E5%BC%95"><span class="toc-number">1.0.3.0.3.</span> <span class="toc-text">索引</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E4%BF%AE%E6%94%B9"><span class="toc-number">1.0.3.0.4.</span> <span class="toc-text">修改</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E9%81%8D%E5%8E%86"><span class="toc-number">1.0.3.0.5.</span> <span class="toc-text">遍历</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%86%85%E5%AD%98%E6%A8%A1%E5%9E%8B"><span class="toc-number">1.0.3.0.6.</span> <span class="toc-text">内存模型</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%80%BC%E7%B1%BB%E5%9E%8B"><span class="toc-number">1.0.3.0.7.</span> <span class="toc-text">值类型</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9B%9B%E3%80%81%E5%88%87%E7%89%87"><span class="toc-number">1.0.4.</span> <span class="toc-text">四、切片</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%88%87%E7%89%87%E5%88%A0%E9%99%A4%EF%BC%88%E4%BC%AA%E5%88%A0%E9%99%A4%EF%BC%89"><span class="toc-number">1.0.4.0.1.</span> <span class="toc-text">切片删除（伪删除）</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%95%BF%E5%BA%A6%E5%92%8C%E5%AE%B9%E9%87%8F-1"><span class="toc-number">1.0.4.1.</span> <span class="toc-text">长度和容量</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%BC%95%E7%94%A8%E7%B1%BB%E5%9E%8B"><span class="toc-number">1.0.4.1.1.</span> <span class="toc-text">引用类型</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%88%AA%E5%8F%96%E5%AD%90%E5%88%87%E7%89%87"><span class="toc-number">1.0.4.1.2.</span> <span class="toc-text">截取子切片</span></a></li></ol></li></ol></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/dujie5416/2023/07/29/%E6%A0%B9%E6%8D%AErouter%E6%98%BE%E7%A4%BA%E5%B7%A6%E4%BE%A7%E8%8F%9C%E5%8D%95%E6%A0%8F%E9%9A%90%E8%97%8F%E6%9F%90%E4%B8%AA%E8%8F%9C%E5%8D%95%E9%80%89%E9%A1%B9/" title="vue3左侧菜单隐藏某选项"><img src="/dujie5416/img/golang.png" onerror="this.onerror=null;this.src='/dujie5416/img/404.jpg'" alt="vue3左侧菜单隐藏某选项"/></a><div class="content"><a class="title" href="/dujie5416/2023/07/29/%E6%A0%B9%E6%8D%AErouter%E6%98%BE%E7%A4%BA%E5%B7%A6%E4%BE%A7%E8%8F%9C%E5%8D%95%E6%A0%8F%E9%9A%90%E8%97%8F%E6%9F%90%E4%B8%AA%E8%8F%9C%E5%8D%95%E9%80%89%E9%A1%B9/" title="vue3左侧菜单隐藏某选项">vue3左侧菜单隐藏某选项</a><time datetime="2023-07-29T06:32:00.000Z" title="发表于 2023-07-29 14:32:00">2023-07-29</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/dujie5416/2023/07/13/Vue%E7%8A%B6%E6%80%81%E7%AE%A1%E7%90%86/" title="Vue状态管理"><img src="/dujie5416/img/golang.png" onerror="this.onerror=null;this.src='/dujie5416/img/404.jpg'" alt="Vue状态管理"/></a><div class="content"><a class="title" href="/dujie5416/2023/07/13/Vue%E7%8A%B6%E6%80%81%E7%AE%A1%E7%90%86/" title="Vue状态管理">Vue状态管理</a><time datetime="2023-07-13T09:29:00.000Z" title="发表于 2023-07-13 17:29:00">2023-07-13</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/dujie5416/2023/06/27/%E5%9F%BA%E4%BA%8EIOC%E8%A7%A3%E5%86%B3%E5%AF%B9%E8%B1%A1%E4%BE%9D%E8%B5%96%E9%97%AE%E9%A2%98/" title="基于IOC解决对象依赖问题"><img src="/dujie5416/img/golang.png" onerror="this.onerror=null;this.src='/dujie5416/img/404.jpg'" alt="基于IOC解决对象依赖问题"/></a><div class="content"><a class="title" href="/dujie5416/2023/06/27/%E5%9F%BA%E4%BA%8EIOC%E8%A7%A3%E5%86%B3%E5%AF%B9%E8%B1%A1%E4%BE%9D%E8%B5%96%E9%97%AE%E9%A2%98/" title="基于IOC解决对象依赖问题">基于IOC解决对象依赖问题</a><time datetime="2023-06-27T03:32:00.000Z" title="发表于 2023-06-27 11:32:00">2023-06-27</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/dujie5416/2023/06/23/Gin%E6%A1%86%E6%9E%B6/" title="Gin框架"><img src="/dujie5416/img/golang.png" onerror="this.onerror=null;this.src='/dujie5416/img/404.jpg'" alt="Gin框架"/></a><div class="content"><a class="title" href="/dujie5416/2023/06/23/Gin%E6%A1%86%E6%9E%B6/" title="Gin框架">Gin框架</a><time datetime="2023-06-23T06:32:00.000Z" title="发表于 2023-06-23 14:32:00">2023-06-23</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/dujie5416/2023/06/04/ServeMux%E3%80%81httprouter/" title="ServeMux、httprouter"><img src="/dujie5416/img/golang.png" onerror="this.onerror=null;this.src='/dujie5416/img/404.jpg'" alt="ServeMux、httprouter"/></a><div class="content"><a class="title" href="/dujie5416/2023/06/04/ServeMux%E3%80%81httprouter/" title="ServeMux、httprouter">ServeMux、httprouter</a><time datetime="2023-06-04T03:32:00.000Z" title="发表于 2023-06-04 11:32:00">2023-06-04</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('/dujie5416/img/longzhu.png')"><div id="footer-wrap"><div class="copyright">&copy;2019 - 2024  <i id="heartbeat" class="fa fas fa-heartbeat"></i> J.</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a><a href="http://beian.miit.gov.cn/"  style="color:#f72b07" target="_blank">京ICP备2022023567号</a></div></div><link rel="stylesheet" href="https://fastly.jsdelivr.net/gh/HCLonely/images@master/others/heartbeat.min.css"></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="algolia-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="search-wrap"><div id="algolia-search-input"></div><hr/><div id="algolia-search-results"><div id="algolia-hits"></div><div id="algolia-pagination"></div><div id="algolia-info"><div class="algolia-stats"></div><div class="algolia-poweredBy"></div></div></div></div></div><div id="search-mask"></div></div><div id="rightMenu"><div class="rightMenu-group rightMenu-small"><div class="rightMenu-item" id="menu-backward"><i class="fa-solid fa-arrow-left"></i></div><div class="rightMenu-item" id="menu-forward"><i class="fa-solid fa-arrow-right"></i></div><div class="rightMenu-item" id="menu-refresh"><i class="fa-solid fa-arrow-rotate-right"></i></div><div class="rightMenu-item" id="menu-home"><i class="fa-solid fa-house"></i></div></div><div class="rightMenu-group rightMenu-line rightMenuOther"><a class="rightMenu-item menu-link" href="/archives/"><i class="fa-solid fa-archive"></i><span>文章归档</span></a><a class="rightMenu-item menu-link" href="/categories/"><i class="fa-solid fa-folder-open"></i><span>文章分类</span></a><a class="rightMenu-item menu-link" href="/tags/"><i class="fa-solid fa-tags"></i><span>文章标签</span></a></div><div class="rightMenu-group rightMenu-line rightMenuNormal"><a class="rightMenu-item menu-link" id="menu-radompage" href="/random/index.html"><i class="fa-solid fa-shoe-prints"></i><span>随便逛逛</span></a><div class="rightMenu-item" id="menu-translate"><i class="fa-solid fa-earth-asia"></i><span>繁简切换</span></div><div class="rightMenu-item" id="menu-darkmode"><i class="fa-solid fa-moon"></i><span>切换模式</span></div></div></div><div id="rightmenu-mask"></div><div><script src="/dujie5416/js/utils.js"></script><script src="/dujie5416/js/main.js"></script><script src="/dujie5416/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/algoliasearch/dist/algoliasearch-lite.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/instantsearch.js/dist/instantsearch.production.min.js"></script><script src="/dujie5416/js/search/algolia.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"><script>function loadLivere () {
  if (typeof LivereTower === 'object') {
    window.LivereTower.init()
  }
  else {
    (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
    })(document, 'script');
  }
}

if ('Livere' === 'Livere' || !false) {
  if (false) btf.loadComment(document.getElementById('lv-container'), loadLivere)
  else loadLivere()
}
else {
  function loadOtherComment () {
    loadLivere()
  }
}</script></div><script defer src="https://npm.elemecdn.com/jquery@latest/dist/jquery.min.js"></script><script data-pjax defer src="https://npm.elemecdn.com/tzy-blog/lib/js/theme/chocolate.js"></script><canvas id="universe"></canvas><script defer src="/js/universe.js"></script><script defer data-pjax src="/js/rightMenu.js"></script><div class="aplayer no-destroy" data-id="7427714271" data-server="netease" data-type="playlist" data-fixed="true" data-mini="true" data-listFolded="false" data-order="random" data-lrctype="1" data-preload="none" data-autoplay="true" muted></div><script id="click-show-text" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/click-show-text.min.js" data-mobile="false" data-text="I,LOVE,YOU" data-fontsize="15px" data-random="false" async="async"></script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/metingjs/dist/Meting.min.js"></script><script src="https://cdn.jsdelivr.net/npm/pjax/pjax.min.js"></script><script>let pjaxSelectors = ["meta[property=\"og:image\"]","meta[property=\"og:title\"]","meta[property=\"og:url\"]","head > title","#config-diff","#body-wrap","#rightside-config-hide","#rightside-config-show",".js-pjax"]

var pjax = new Pjax({
  elements: 'a:not([target="_blank"])',
  selectors: pjaxSelectors,
  cacheBust: false,
  analytics: false,
  scrollRestoration: false
})

document.addEventListener('pjax:send', function () {

  // removeEventListener scroll 
  window.tocScrollFn && window.removeEventListener('scroll', window.tocScrollFn)
  window.scrollCollect && window.removeEventListener('scroll', scrollCollect)

  typeof preloader === 'object' && preloader.initLoading()
  document.getElementById('rightside').style.cssText = "opacity: ''; transform: ''"
  
  if (window.aplayers) {
    for (let i = 0; i < window.aplayers.length; i++) {
      if (!window.aplayers[i].options.fixed) {
        window.aplayers[i].destroy()
      }
    }
  }

  typeof typed === 'object' && typed.destroy()

  //reset readmode
  const $bodyClassList = document.body.classList
  $bodyClassList.contains('read-mode') && $bodyClassList.remove('read-mode')

  typeof disqusjs === 'object' && disqusjs.destroy()
})

document.addEventListener('pjax:complete', function () {
  window.refreshFn()

  document.querySelectorAll('script[data-pjax]').forEach(item => {
    const newScript = document.createElement('script')
    const content = item.text || item.textContent || item.innerHTML || ""
    Array.from(item.attributes).forEach(attr => newScript.setAttribute(attr.name, attr.value))
    newScript.appendChild(document.createTextNode(content))
    item.parentNode.replaceChild(newScript, item)
  })

  GLOBAL_CONFIG.islazyload && window.lazyLoadInstance.update()

  typeof chatBtnFn === 'function' && chatBtnFn()
  typeof panguInit === 'function' && panguInit()

  // google analytics
  typeof gtag === 'function' && gtag('config', '', {'page_path': window.location.pathname});

  // baidu analytics
  typeof _hmt === 'object' && _hmt.push(['_trackPageview',window.location.pathname]);

  typeof loadMeting === 'function' && document.getElementsByClassName('aplayer').length && loadMeting()

  // prismjs
  typeof Prism === 'object' && Prism.highlightAll()

  typeof preloader === 'object' && preloader.endLoading()
})

document.addEventListener('pjax:error', (e) => {
  if (e.request.status === 404) {
    pjax.loadUrl('/404.html')
  }
})</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><!-- hexo injector body_end start --><script data-pjax>
  function butterfly_clock_anzhiyu_injector_config(){
    var parent_div_git = document.getElementsByClassName('sticky_layout')[0];
    var item_html = '<div class="card-widget card-clock"><div class="card-glass"><div class="card-background"><div class="card-content"><div id="hexo_electric_clock"><img class="entered loading" id="card-clock-loading" src="https://cdn.cbd.int/hexo-butterfly-clock-anzhiyu/lib/loading.gif" style="height: 120px; width: 100%;" data-ll-status="loading"/></div></div></div></div></div>';
    console.log('已挂载butterfly_clock_anzhiyu')
    if(parent_div_git) {
      parent_div_git.insertAdjacentHTML("afterbegin",item_html)
    }
  }
  var elist = 'null'.split(',');
  var cpage = location.pathname;
  var epage = 'all';
  var qweather_key = '56289903f0d448dfbe8262dba0175f70';
  var gaud_map_key = '01603f2b2d08d0e947bb101e9d9b148b';
  var baidu_ak_key = 'undefined';
  var flag = 0;
  var clock_rectangle = '112.982279,28.19409';
  var clock_default_rectangle_enable = 'false';

  for (var i=0;i<elist.length;i++){
    if (cpage.includes(elist[i])){
      flag++;
    }
  }

  if ((epage ==='all')&&(flag == 0)){
    butterfly_clock_anzhiyu_injector_config();
  }
  else if (epage === cpage){
    butterfly_clock_anzhiyu_injector_config();
  }
  </script><script src="https://widget.qweather.net/simple/static/js/he-simple-common.js?v=2.0"></script><script data-pjax src="https://cdn.cbd.int/hexo-butterfly-clock-anzhiyu/lib/clock.min.js"></script><!-- hexo injector body_end end --><script src="/dujie5416/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"tagMode":false,"debug":false,"model":{"jsonPath":"/dujie5416/live2dw/assets/tororo.model.json"},"display":{"position":"right","width":150,"height":350,"hOffset":20,"vOffset":-20},"mobile":{"show":false},"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/"});</script><script async>window.onload=function(){var a=document.createElement('script'),b=document.getElementsByTagName('script')[0];a.type='text/javascript',a.async=!0,a.src='/sw-register.js?v='+Date.now(),b.parentNode.insertBefore(a,b)};</script></body></html>